package com.j1ay.forum.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.j1ay.forum.dao.mapper.AnswerMapper;
import com.j1ay.forum.dao.mapper.QuestionMapper;
import com.j1ay.forum.model.pojo.po.Answer;
import com.j1ay.forum.model.pojo.po.Question;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author J1ay
 * @Date 2021/12/17 14:58
 * @Description 在线程池进行操作，不会影响主线程
 */

@Component
public class ThreadService {
    @Async("taskExecutor")
    public void updateViewCount(QuestionMapper questionMapper, Question question) {
        int viewCount = question.getViewCount();
        Question question1 = new Question();
        question1.setViewCount(viewCount+1);
        LambdaUpdateWrapper<Question> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Question::getId, question.getId());
        updateWrapper.eq(Question::getViewCount, viewCount);
        questionMapper.update(question1, updateWrapper);

        try {
            Thread.sleep(5000);
            System.out.println("浏览问题更新完成了...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点赞该回答
     * @param answerMapper
     * @param answer
     */
    @Async("taskExecutor")
    public void updateANswerPraise(AnswerMapper answerMapper, Answer answer) {
        int praise = answer.getPraise();
        answer.setPraise(praise+1);
        LambdaUpdateWrapper<Answer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Answer::getId, answer.getId());
        updateWrapper.eq(Answer::getPraise, praise);
        answerMapper.update(answer, updateWrapper);

        try {
            Thread.sleep(5000);
            System.out.println("点赞问题更新完成了...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点赞问题
     * @param questionMapper
     * @param question
     */
    @Async("taskExecutor")
    public void updatePraise(QuestionMapper questionMapper, Question question) {
        int praise = question.getPraise();
        Question question1 = new Question();
        question1.setPraise(praise+1);
        LambdaUpdateWrapper<Question> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Question::getId, question.getId());
        updateWrapper.eq(Question::getPraise, praise);
        questionMapper.update(question1, updateWrapper);

        try {
            Thread.sleep(5000);
            System.out.println("点赞更新完成了...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新问题回答数
     * @param questionMapper
     * @param question
     */
    @Async("taskExecutor")
    public void updateAnswerCount(QuestionMapper questionMapper, Question question) {
        int answerCount = question.getAnswerCount();
        Question question1 = new Question();
        question1.setAnswerCount(answerCount+1);
        LambdaUpdateWrapper<Question> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Question::getId, question.getId());
        updateWrapper.eq(Question::getAnswerCount, answerCount);
        questionMapper.update(question1, updateWrapper);

        try {
            Thread.sleep(5000);
            System.out.println("问题回答数更新完成了...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
